<?php

/**
 * main actions.
 *
 * @package    vo-ser
 * @subpackage main
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class mainActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $r)
    {
        $user = $this->getUser();
        if (!$user->isAuthenticated())
        	$this->redirect('@login');
		
		if ($user->hasCredential('admin'))
		{
			$this->projects = Doctrine::getTable('Project')->findAll();
			$this->admin = true;
		}
		else
		{
			$this->ownedProjects = $user->getUserObject()->getOwnedProjects();
			$this->memberProjects = $user->getUserObject()->getMemberProjects();
		}
        
		$this->flash = $this->getUser()->getFlash('flash');
    }

    public function executeLogin(sfWebRequest $r)
    {
        if ($this->getUser()->isAuthenticated())
        $this->redirect('@homepage');

        $this->form = new LoginForm();
        $this->errors = array ();

        if ($r->isMethod('put'))
        {
            $this->form->bind($r->getParameter('login'));

            if ($this->form->isValid())
            {
                $username = $this->form->getValue('username');
                $password = $this->form->getValue('password');

                $user = Doctrine::getTable('User')->checkUser($username, $password);

                if ($user)
                {
                    $this->getUser()->setAttribute('id', $user->getID());
					
					if ($user->isAdmin)
                    	$this->getUser()->addCredential('admin');

                    $this->getUser()->setAuthenticated(true);
                    $this->redirect('@homepage');
                }

                $this->errors[] = 'Username or password incorrect, please try again.';
            }
        }
    }

    public function executeLogout(sfWebRequest $r)
    {
        $this->getUser()->getAttributeHolder()->clear();
		$this->getUser()->clearCredentials();
        $this->getUser()->setAuthenticated(false);
        $this->redirect('@homepage');
    }

    public function executeSecure(sfWebRequest $r)
    {

    }

    public function executeCreateProject(sfWebRequest $r)
    {	  
	    $this->form = new ProjectForm();

        if ($r->isMethod('POST'))
        {
            $this->form->bind($r->getParameter($this->form->getName()));

            if ($this->form->isValid())
            {
                $this->form->save();
				$this->getUser()->setFlash('flash', "Project {$this->form->getObject()->getName()} created");
                $this->redirect('@homepage');
            }
        }
    }
	
	public function executeEditProject(sfWebRequest $r)
	{
		$project = Doctrine::getTable('Project')->find($r->getParameter('id'));
		$user = $this->getUser()->getUserObject();
		
		if (!$this->getUser()->hasCredential('admin') && $project->getOwner()->getId() != $user->getId())
			$this->redirect('@homepage');
		
		$this->form = new ProjectForm($project);

        if ($r->isMethod('POST'))
        {
            $this->form->bind($r->getParameter($this->form->getName()));

            if ($this->form->isValid())
            {
                $this->form->save();
				$this->getUser()->setFlash('flash', "Project {$this->form->getObject()->getName()} edited");
                $this->redirect('@homepage');
            }
        }
	}

    public function executeCreateUser(sfWebRequest $r)
    {
        if (!$this->getUser()->hasCredential('admin'))
			$this->redirect('@homepage');
		
		$this->form = new UserForm();
		
		if ($r->isMethod('POST'))
		{
			$this->form->bind($r->getParameter($this->form->getName()));
			
			if ($this->form->isValid())
			{
				$this->form->save();
				$this->getUser()->setFlash('flash', "User {$this->form->getObject()->getName()} was created");
                $this->redirect('@user_list');
			}
		}
    }
	
	public function executeEditUser(sfWebRequest $r)
	{
		if (!$this->getUser()->hasCredential('admin'))
			$this->redirect('@homepage');
		
		$userID = $r->getParameter('id');
		$user = Doctrine::getTable('User')->find($userID);
		$this->form = new UserForm($user);
		
		if ($r->isMethod('POST'))
		{
			$this->form->bind($r->getParameter($this->form->getName()));
			
			if ($this->form->isValid())
			{
				$this->form->save();
				$this->getUser()->setFlash('flash', "User {$this->form->getObject()->getName()} was edited");
                $this->redirect('@user_list');
			}
		}
	}
	
	public function executeDeleteUser(sfWebRequest $r)
	{
		if (!$this->getUser()->hasCredential('admin'))
			$this->redirect('@homepage');
		
		$userID = $r->getParameter('id');
		$user = Doctrine::getTable('User')->find($userID);
		$name = $user->getName();
		$user->delete();
		
		$this->getUser()->setFlash('flash', "User $name was deleted");
        $this->redirect('@user_list');
	}
	
	public function executeUserList(sfWebRequest $r)
	{
		$this->users = Doctrine::getTable('User')->findAll();
		$this->flash = $this->getUser()->getFlash('flash');
		$this->admin = $this->getUser()->hasCredential('admin');
	}
}
